package com.arcgis.app.controller.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.arcgis.app.MainActivity;
import com.arcgis.app.R;
import com.arcgis.app.utils.StringUtils;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.ImmutablePart;
import com.esri.arcgisruntime.geometry.Multipart;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchGeometryChangedEvent;
import com.esri.arcgisruntime.mapping.view.SketchGeometryChangedListener;
import com.esri.arcgisruntime.symbology.TextSymbol;

import butterknife.BindView;
import butterknife.ButterKnife;

//import android.graphics.Point;


public class FragmentTool extends Fragment {
    @BindView(R.id.lv_point)
    LinearLayout lvPoint;
    @BindView(R.id.lv_line)
    LinearLayout lvLine;
    @BindView(R.id.lv_area)
    LinearLayout lvArea;
    @BindView(R.id.lv_next)
    LinearLayout lvNext;
    @BindView(R.id.lv_chexiao)
    LinearLayout lvChexiao;
    @BindView(R.id.lv_del)
    LinearLayout lvDel;

    public SketchEditor sketchEditor;
    @BindView(R.id.point_label)
    TextView pointLabel;
    @BindView(R.id.lv_close)
    LinearLayout lvClose;
    @BindView(R.id.image_choose_sketch)
    ImageView imageChooseSketch;
    @BindView(R.id.btn_choose_sketch)
    TextView btnChooseSketch;

    public enum ToolPointType {
        TOOL_POINT_FREE,
        TOOL_POINT_CENTER
    }

    public ToolPointType pointType;


    private SketchCreationMode curMode = SketchCreationMode.POINT;

    private LinearLayout lastSelectLayout;

    private String mFrom;

    private MainActivity mainActivity() {
        return (MainActivity) getActivity();
    }

    public void setSketchEditor(SketchEditor sketchEditor) {
        this.sketchEditor = sketchEditor;

        if (this.sketchEditor != null) {

            changeMode();
        }
    }

    public SketchCreationMode getCurMode() {
        return curMode;
    }

    private void changeMode() {
        System.out.println("-------------------");
        if (sketchEditor != null) {

            sketchEditor.start(curMode);
            SketchGeometryChangedListener sketchGeometryChangedListener = new SketchGeometryChangedListener() {
                @Override
                public void geometryChanged(SketchGeometryChangedEvent event) {
                    Point displayPoint = null;
                    Geometry geometry = event.getGeometry();
                    if (geometry.getGeometryType() == GeometryType.POINT) {

                        displayPoint = (Point) geometry;

                    } else if (geometry instanceof Multipart) {

                        Multipart multipart = (Multipart) geometry;

                        if (multipart.getParts().isEmpty()) {
                            return;
                        }

                        ImmutablePart part = multipart.getParts().get(multipart.getParts().size() - 1);

                        if (part.getPointCount() == 0) return;

                        displayPoint = part.getPoint(part.getPointCount() - 1);
                        String areaStr = StringUtils.geometryToStr(geometry);
                        TextSymbol textSymbol = new TextSymbol();
                        textSymbol.setText(areaStr);
                        textSymbol.setOffsetY(20);
                        textSymbol.setColor(Color.parseColor("#FF0000"));
                        textSymbol.setSize(20);
//                        Graphic graphic1 = new Graphic(displayPoint, textSymbol);
                        Graphic graphic1 = new Graphic(displayPoint);
                        mainActivity().curPosOverlay.getGraphics().clear();
                        mainActivity().curPosOverlay.getGraphics().add(graphic1);

                    }

                }
            };
            sketchEditor.addGeometryChangedListener(sketchGeometryChangedListener);
        }

    }

    public void changePointMode() {
        if (this.pointType == ToolPointType.TOOL_POINT_FREE) {
            pointType = ToolPointType.TOOL_POINT_CENTER;
            pointLabel.setText("中心点");
        } else {
            pointType = ToolPointType.TOOL_POINT_FREE;
            pointLabel.setText("自由点");
        }

        mainActivity().changePointMode();
    }

    public FragmentTool() {
        // Required empty public constructor
    }

    public static FragmentTool newInstance(String from) {
        FragmentTool fragment = new FragmentTool();
//        Bundle bundle = new Bundle();
//        bundle.putString("from", from);
//        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            mFrom = getArguments().getString("from");
//        }

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        lastSelectLayout = lvPoint;
        pointType = ToolPointType.TOOL_POINT_FREE;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_tool2, container, false);
        ButterKnife.bind(this, view);
//        lvPoint.setSelected(true);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lvPoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mainActivity().beginDraw();
                if (!canOpenTools()){
                    Toast.makeText(getContext(),"请先打开工具",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (lastSelectLayout != null) {

                    lastSelectLayout.setSelected(false);
                }
                mainActivity().setSelectType("point");
                lvPoint.setSelected(true);
                lastSelectLayout = lvPoint;
                curMode = SketchCreationMode.POINT;
                changeMode();
                changePointMode();
//                mainActivity().endDraw();
                Log.i("tag", "point------");
            }
        });

        lvLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!canOpenTools()){
                    Toast.makeText(getContext(),"请先打开工具",Toast.LENGTH_SHORT).show();
                    return;
                }
//                mainActivity().beginDraw();
                if (lastSelectLayout != null) {

                    lastSelectLayout.setSelected(false);
                }
                mainActivity().setSelectType("line");
                lvLine.setSelected(true);
                lastSelectLayout = lvLine;
                curMode = SketchCreationMode.POLYLINE;
                changeMode();
                turnPointStatus();
            }
        });

        lvArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!canOpenTools()){
                    Toast.makeText(getContext(),"请先打开工具",Toast.LENGTH_SHORT).show();
                    return;
                }
//                mainActivity().beginDraw();
                if (lastSelectLayout != null) {

                    lastSelectLayout.setSelected(false);
                }
                mainActivity().setSelectType("area");
                lvArea.setSelected(true);
                lastSelectLayout = lvArea;
                curMode = SketchCreationMode.POLYGON;
                changeMode();
                turnPointStatus();
            }
        });

        lvChexiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivity().redo();
            }
        });

        lvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivity().delete();
            }
        });

        lvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivity().next();
            }
        });

        lvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnChooseSketch.getText().toString().equals(getString(R.string.open))){
                    mainActivity().beginDraw();
                    btnChooseSketch.setText(R.string.close);
                    imageChooseSketch.setImageDrawable(getResources().getDrawable(R.drawable.close_sk));
                    lastSelectLayout = lvPoint;
                    lvPoint.setSelected(true);
                }else{
                    mainActivity().endDraw();
                    mainActivity().addMapListener();
                    btnChooseSketch.setText(R.string.open);
                    imageChooseSketch.setImageDrawable(getResources().getDrawable(R.drawable.open));
                    lastSelectLayout.setSelected(false);
                    lvPoint.setSelected(false);
                }

                turnPointStatus();
//                mainActivity().close();
            }
        });

    }

    public Boolean canOpenTools(){
        if (btnChooseSketch.getText().toString().equals(getString(R.string.close)))
            return true;
        return false;
    }

    public void turnPointStatus() {
        if (pointType == ToolPointType.TOOL_POINT_CENTER) {
            changePointMode();
        }


    }



    @Override
    public void onPause() {
        super.onPause();
    }

//    @OnClick(R.id.lv_point)
//    public void onViewClicked() {
//    }
}
